package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.base.ResultMessage;
//import com.symone.webcms.schedule.JobFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.JobConfigService;
import com.symone.webcms.model.JobConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description :  计划任务管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-04-02
 */
@RestController
@Api(value="/jobConfig", description="计划任务管理")
@RequestMapping("/api/job")
public class JobController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(JobController.class);

    @Autowired
    public JobConfigService jobConfigService;

    /**
     * @description : 获取计划任务分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-02
     */
    @RequiresPermissions("sys:job:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取计划任务分页列表", notes="获取计划任务分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Integer pageSize,Integer pageNum) {
        Wrapper<JobConfig> ew = new EntityWrapper<JobConfig>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<JobConfig> page = new Page<JobConfig>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = jobConfigService.findPage(page, ew);

        return ResultMessage.success("获取计划任务的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有计划任务记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-02
     */
    @RequiresPermissions("sys:job:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有计划任务信息", notes="获取所有计划任务信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有计划任务信息",jobConfigService.findAll());
    }

    /**
     * @description : 通过id获取计划任务
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-02
     */
    @RequiresPermissions("sys:job:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取计划任务信息", notes="获取计划任务信息")
    @ApiImplicitParam(name="id",value="计划任务ID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取计划任务信息成功!",jobConfigService.selectById(id));
    }

    /**
     * @description : 添加计划任务
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-02
     */
    @RequiresPermissions("sys:job:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加计划任务信息", notes="添加计划任务信息")
    @ApiImplicitParam(name = "jobConfig", value = "计划任务信息", required = true )
    public ResultMessage add(JobConfig jobConfig){
        jobConfigService.insert(jobConfig);
        //asyncSchedule();
        return ResultMessage.success("添加计划任务成功!");
    }

    /**
     * @description : 通过id逻辑删除计划任务
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-02
     */
    @RequiresPermissions("sys:job:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除计划任务", notes="逻辑删除计划任务")
    @ApiImplicitParam(name = "id", value = "计划任务Id", required = true )
    public ResultMessage removeFlag(Long id){
        if(jobConfigService.deleteFlagById(id)) {
            //asyncSchedule();
            return ResultMessage.success("逻辑计划任务删除成功！");
        } else {
            return ResultMessage.fail("逻辑计划任务删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除计划任务
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-02
     */
    @RequiresPermissions("sys:job:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除 计划任务", notes="物理删除计划任务")
    @ApiImplicitParam(name = "id", value = "计划任务Id", required = true )
    public ResultMessage remove(long id){
        if(jobConfigService.deleteById(id)) {
            //asyncSchedule();
            return ResultMessage.success("物理删除计划任务成功！");
        } else {
            return ResultMessage.fail("物理删除计划任务失败！");
        }
    }

    /**
     * @description : 更新 计划任务信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-02
     */
    @RequiresPermissions("sys:job:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value="更新计划任务信息", notes="更新计划任务信息")
    @ApiImplicitParam(name = "jobConfig", value = "计划任务信息", required = true )
    public ResultMessage update(JobConfig jobConfig) {
        if(jobConfigService.updateById(jobConfig)) {
            //asyncSchedule();
            return ResultMessage.success("计划任务信息更新成功!");
        }else{
            return ResultMessage.fail("计划任务信息更新失败!");
        }
    }

    /**
     * @description : 更新 计划任务信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-02
     */
    @RequiresPermissions("sys:job:edit")
    @RequestMapping(value = "/batchUpdate", method = RequestMethod.POST)
    @ApiOperation(value="更新计划任务信息", notes="更新计划任务信息")
    @ApiImplicitParam(name = "jobConfig", value = "计划任务信息", required = true )
    public ResultMessage batchUpdate(String ids,Integer status) {
        if(jobConfigService.batchUpdate(ids,status)) {
            //asyncSchedule();
            return ResultMessage.success("计划任务信息更新成功!");
        }else{
            return ResultMessage.fail("计划任务信息更新失败!");
        }
    }

    /**
    * @description : 清除计划任务记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-02
    */
    @RequiresPermissions("sys:job:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除计划任务记录", notes="清除已被逻辑删除的计划任务记录")
    public ResultMessage clear(){
        int num = jobConfigService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有计划任务记录被清除！");
        }
    }

//    private void asyncSchedule(){
//        try {
//            JobFactory.updateSchedule();
//        }catch (Exception ex){
//            logger.debug(ex.getMessage());
//        }
//    }

}
